package com.htfyun.roobofactory.factorytest;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.widget.TextView;

import com.htfyun.roobofactory.R;
import com.htfyun.roobofactory.config.NVRomKeys;
import com.htfyun.roobofactory.config.RooboBuildConfig;
import com.htfyun.roobofactory.htfyunUtils.NvDataTool;

/**
 * Created by sst on 2018/7/7.
 */

public enum FactoryTestItemsEnum {

    test_lcd(0, NVRomKeys.KEY_TEST_LCD, R.string.test_lcd, R.string.test_lcd_en, LcdTestActivity.class),
    test_tp(10, NVRomKeys.KEY_TEST_TP, R.string.test_tp, R.string.test_tp_en, TPTestActivity.class),
    test_camera(20, NVRomKeys.KEY_TEST_CAMERA, R.string.test_camera, R.string.test_camera_en, CameraTestActivity.class),
    test_speaker(30, NVRomKeys.KEY_TEST_SPEAKER, R.string.test_speaker, R.string.test_speaker_en, SpeakerTestActivity.class),
    test_mic(40, NVRomKeys.KEY_TEST_MIC, R.string.test_mic, R.string.test_mic_en, MicTestActivity.class),
    test_light_sensor(50, NVRomKeys.KEY_TEST_LIGHT_SENSOR, R.string.test_light_sensor, R.string.test_light_sensor_en, LightSensorTestActivity.class),
    test_proximity_sensor(60, NVRomKeys.KEY_TEST_PROXIMITY_SENSOR, R.string.test_proximity_sensor, R.string.test_proximity_sensor_en, ProximitySensorTestActivity.class),
    test_keys(70, NVRomKeys.KEY_TEST_KEYS, R.string.test_key, R.string.test_key_en, KeysTestActivity.class),
    test_led(80, NVRomKeys.KEY_TEST_LED, R.string.test_led, R.string.test_led_en, LedTestActivity.class),
    test_battery(90, NVRomKeys.KEY_TEST_BATTERY, R.string.test_battery, R.string.test_battery_en, BatteryTestActivity.class),
    test_sdcard(100, NVRomKeys.KEY_TEST_SDCARD, R.string.test_sdcard, R.string.test_sdcard_en, SDCardTestActivity.class),
    test_bt(110, NVRomKeys.KEY_TEST_BT, R.string.test_bt, R.string.test_bt_en, BluetoothTestActivity.class),
    test_wifi(120, NVRomKeys.KEY_TEST_WIFI, R.string.test_wifi, R.string.test_wifi_en, WifiTestActivity.class),
    test_flash_and_memory(130, NVRomKeys.KEY_TEST_FLASH_AND_MEMORY, R.string.test_flash_and_memory, R.string.test_flash_and_memory_en, FlashAndMemoryTestActivity.class),
    test_wakeup(140, NVRomKeys.KEY_TEST_WAKEUP, R.string.test_wakeup, R.string.test_wakeup_en, WakeupTestActivity.class),
    ;

    private final int orderId;//orderId 不能一样, 这个会用于启动activity的requestCode
    private final String nvromKey;
    private final int nameResId;
    private final int nameEnResId;
    private final Class<? extends Activity> ItemClass;

    private final static String SHARED_PREFERENCES_NAME = "factoryTestItems";

    private FactoryTestItemsEnum(int order, String nvromKey, int nameResId, int nameEnResId, Class<? extends Activity> cls) {
        this.orderId = order;
        this.nvromKey = nvromKey;
        this.nameResId = nameResId;
        this.nameEnResId = nameEnResId;
        ItemClass = cls;
    }

    public int getOrderId() {
        return orderId;
    }

    public String getNVRomKey() {
        return nvromKey;
    }

    public int getNameResId() {
        return nameResId;
    }

    public int getNameEnResId() {
        return nameEnResId;
    }

    public Class getItemClass() {
        return ItemClass;
    }

  /*  public String getResult(Context context, FactoryTestItemsEnum itemsEnum) {
        SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
        String key = itemsEnum.getItemClass().getCanonicalName();
        String defResult = context.getResources().getString(R.string.test_result_unknown);
        return sp.getString(key, defResult);
    }

    public void setResult(Context context, FactoryTestItemsEnum itemsEnum, int resultResId) {
        SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        String key = itemsEnum.getItemClass().getCanonicalName();
        String result = context.getResources().getString(resultResId);
        editor.putString(key, result);
        editor.apply();
    }*/

    public String getResult(Context context) {

        String key = getNVRomKey();
        String defResult = context.getResources().getString(R.string.test_result_unknown);

        if (RooboBuildConfig.SAVE_TEST_RESULT_INTO_NVROM) {

            String result = NvDataTool.getInstance().readNVRomString(key);

            if (TextUtils.isEmpty(result)) {
                result = defResult;
            }

            return result;

        } else {

            SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);

            return sp.getString(key, defResult);
        }
    }

    public void setResult(Context context, String result) {

        String key = getNVRomKey();

        if (RooboBuildConfig.SAVE_TEST_RESULT_INTO_NVROM) {

            NvDataTool.getInstance().saveNVRomString(key, result);

        } else {

            SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();

            editor.putString(key, result);
            editor.apply();
        }


    }

    public boolean isPassed(Context context) {
        String result = getResult(context);
        String pass = context.getString(R.string.test_result_pass);
        return !TextUtils.isEmpty(result) && !TextUtils.isEmpty(pass) && result.equals(pass);
    }



    public int getColor(Context context) {
        String result = getResult(context);
        String pass = context.getString(R.string.test_result_pass);
        String fail = context.getString(R.string.test_result_fail);
        String unknown = context.getString(R.string.test_result_unknown);

        if (result.equals(pass)) {
            return context.getColor(R.color.colorGreen);
        } else if (result.equals(fail)) {
            return context.getColor(R.color.colormRedbg);
        }

        return context.getColor(R.color.colorWhite);
    }

    public String getLog(Context context) {
        String key = getNVRomKey()+"-log";
        String defResult = context.getResources().getString(R.string.test_result_autopass);

        if (RooboBuildConfig.SAVE_TEST_RESULT_INTO_NVROM) {

            String result = NvDataTool.getInstance().readNVRomString(key);

            if (TextUtils.isEmpty(result)) {
                result = defResult;
            }

            return result;


        } else {

            SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);

            return sp.getString(key, defResult);
        }

    }
    public void setLog(Context context, String log) {
        String key = getNVRomKey()+"-log";

        if (RooboBuildConfig.SAVE_TEST_RESULT_INTO_NVROM) {

            NvDataTool.getInstance().saveNVRomString(key, log);

        } else {
            SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            String result = log;
            editor.putString(key, result);
            editor.apply();
        }

    }



}
